/**
 * 
 */
package com.jdy.haoduoaiteacher.activity;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.jdy.haoduoaiteacher.MyTeacherApp;
import com.jdy.haoduoaiteacher.adapter.ImageListAdapter;
import com.jdy.haoduoaiteacher.intface.IAsyncTask;
import com.jdy.haoduoaiteacher.model.ImageItem;
import com.jdy.haoduoaiteacher.model.LeUser;
import com.jdy.haoduoaiteacher.util.CustomAsyncTask;
import com.jdy.haoduoaiteacher.util.HttpUtils;
import com.jdy.haoduoaiteacher.util.IntentConstants;
import com.jdy.haoduoaiteacher.util.ResponseResult;
import com.jdy.haoduoaiteacher.util.Tools;
import com.jdy.haoduoaiteacher.widget.CustomCommonDialog;
import com.jdy.haoduoaiteacher.widget.PopWindow;
import com.jdy.haoduoaiteacher.widget.PopWindow.OnMenuClickListener;
import com.ycsj.goldmedalnewconcept.R;

import org.droidparts.annotation.inject.InjectView;

import java.util.ArrayList;
import java.util.HashMap;

//import com.jdy.haoduoaiteacher.R;

public class WallPicActivity extends BaseActivity implements
        OnMenuClickListener {

  @InjectView(id = R.id.listView1)
  private ListView listView1;

  private ImageListAdapter mAdapter;
  private ArrayList<ImageItem> imagelist;

  @SuppressWarnings("unchecked")
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_wallpic_layout_teacher);

    setTitle("照片墙");

    img_upright.setVisibility(View.VISIBLE);
    img_upright.setOnClickListener(new OnClickListener() {

      @Override
      public void onClick(View v) {
        PopWindow popWindow = new PopWindow(WallPicActivity.this);
        popWindow.setOnMenuClickListener(WallPicActivity.this);
        popWindow.showPopupWindow(img_upright);
      }
    });

    imagelist = (ArrayList<ImageItem>) getIntent().getSerializableExtra(
        "imagelist");

    mAdapter = new ImageListAdapter(this, imagelist);
    listView1.setAdapter(mAdapter);
    listView1.setOnItemClickListener(new OnItemClickListener() {

      @Override
      public void onItemClick(AdapterView<?> parent, View view, int position,
          long id) {
        Intent intent = new Intent(WallPicActivity.this,
            ImageZoomActivity2.class);
        intent.putExtra(IntentConstants.EXTRA_IMAGE_LIST, imagelist);
        intent.putExtra(IntentConstants.EXTRA_CURRENT_IMG_POSITION, position);
        startActivity(intent);
      }

    });
    mAdapter.notifyDataSetChanged();
  }

  @Override
  protected void onDestroy() {
    Intent resultIntent = new Intent();
    setResult(RESULT_CANCELED, resultIntent);

    finish();

    super.onDestroy();
  }

  @Override
  public void onClickShare() {
    Intent intent = new Intent(this, SharePopupWindow.class);
    String url = "http://www.xiaoxiaoban.com.cn/shareAlbum_hda/shareAlbum.html?pid="
        + imagelist.get(0).photo_cloud_id;
    intent.putExtra("Sharedlink", url);
    startActivity(intent);
  }

  @Override
  public void onClickDelete() {
    CustomCommonDialog.Builder cdb = new CustomCommonDialog.Builder(this)
        .setMessage("确定要删除吗？删除后不可恢复。").setTitle("删除提示")
        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
          @Override
          public void onClick(DialogInterface dialog, int which) {
            dialog.dismiss();
          }
        }).setPositiveButton("确定", new DialogInterface.OnClickListener() {
          @Override
          public void onClick(final DialogInterface dialog, int id) {

            new CustomAsyncTask(WallPicActivity.this, new IAsyncTask() {
              @Override
              public void onRecieveData(Activity activity,
                  final ResponseResult arg0) {
                if (arg0.isSuccess()) {
                  dialog.dismiss();

                  Intent resultIntent = new Intent();
                  setResult(RESULT_OK, resultIntent);

                  finish();
                } else {
                  toastShow(arg0.data);
                }

                dismissLoading();
              }

              @Override
              public ResponseResult doInbackground(Activity activity) {
                LeUser lu = MyTeacherApp.getInstance().getUserTeacher();
                if (lu == null) {
                  lu = Tools.restoreLeUser();
                }

                HashMap<String, Object> param_ = new HashMap<String, Object>();
                param_.put(HttpUtils.TAG_SNO_I, lu.sno);

                return HttpUtils.startRequest(HttpUtils.API_URL
                    + "/api/2.0/member/photocloud/"
                    + imagelist.get(0).photo_cloud_id, param_,
                    HttpUtils.TAG_DELETE_REQUEST);
              }
            }).execute();

          }
        });
    cdb.create().show();
  }

  @Override
  public void onClickCancel() {
    // TODO Auto-generated method stub
  }

}
